package com.css.dzj.dzjdzqs.modules.aplaw.service.impl;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjdzqs.common.util.HttpClientUtil;
import com.css.dzj.dzjdzqs.modules.aplaw.entity.ApLawEntity;
import com.css.dzj.dzjdzqs.modules.aplaw.mapper.ApLawMapper;
import com.css.dzj.dzjdzqs.modules.aplaw.service.ApLawService;
import com.css.dzj.dzjdzqs.modules.aplaw.vo.ApLawVo;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author WT
 * @Date 2020/6/29
 */
@Service
public class ApLawServiceImpl implements ApLawService {

    @Autowired
    JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ApLawMapper apLawMapper;
    @Value("${AP_URL}")
    private String apUrl;

    @Override
    @Transactional
    public void add(List<ApLawVo> apLawVoList) {
        try {
            for(ApLawVo apLawVo :apLawVoList){
                ApLawEntity apLawEntity = new ApLawEntity();
                apLawEntity.setPublicId(apLawVo.getPublicId());
                apLawEntity.setLawsType(apLawVo.getLawsType());
                apLawEntity.setLegalName(apLawVo.getTitle());
                apLawEntity.setPublishingAuthority(apLawVo.getOrganName());
                apLawEntity.setReleaseNumber(apLawVo.getReleaseNumber());
                apLawEntity.setReleaseDate(apLawVo.getPublishTime());
                apLawEntity.setDateImplementation(apLawVo.getDateImplementation());
                apLawEntity.setLegalRegulatoryContent(apLawVo.getContent());
                apLawEntity.setKeyword(apLawVo.getKeyword());
                apLawMapper.save(apLawEntity);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    @Transactional
    public void pushtLaw() {

        try{
            String msg = HttpClientUtil.doGet(apUrl+"/publick/getApList");
            List<ApLawVo> apLawVoList = JSON.parseArray(msg, ApLawVo.class);
            if(null!=apLawVoList && apLawVoList.size()>0){
                add(apLawVoList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
